/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.view;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author eric.santos
 */
public class HeaderAndFooter extends PdfPageEventHelper {
    
    private Phrase header;
    private PdfPTable footer;
    private BaseFont helv;  

    public HeaderAndFooter() {

        DateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
        Font font = new Font( Font.FontFamily.TIMES_ROMAN, 10 );

        footer = new PdfPTable(1);
        footer.setTotalWidth(300);
        footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        footer.getDefaultCell().setFollowingIndent(50f);
        footer.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
        footer.getDefaultCell().setBorder(0);
        footer.addCell(new Phrase(new Chunk(sf.format(new Date()),font)));

    }
    
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        try {
            helv = BaseFont.createFont(BaseFont.HELVETICA,
                    BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        if (document.getPageNumber() > 1) {
            ColumnText.showTextAligned(cb,
                    Element.ALIGN_CENTER, getHeader(),
                    (document.right() - document.left()) / 2
                    + document.leftMargin(), document.top() + 10, 0);
        }
        getFooter().writeSelectedRows(0, -1,
                (document.right() - document.left() - 300) / 2
                + document.leftMargin(), document.bottom() - 10, cb);
    }

    /**
     * @return the header
     */
    public Phrase getHeader() {
        return header;
    }

    /**
     * @param header the header to set
     */
    public void setHeader(Phrase header) {
        this.header = header;
    }

    /**
     * @return the footer
     */
    public PdfPTable getFooter() {
        return footer;
    }

    /**
     * @param footer the footer to set
     */
    public void setFooter(PdfPTable footer) {
        this.footer = footer;
    }

    /**
     * @return the helv
     */
    public BaseFont getHelv() {
        return helv;
    }

    /**
     * @param helv the helv to set
     */
    public void setHelv(BaseFont helv) {
        this.helv = helv;
    }
    
}
